package top.hzy520.dao;

import top.hzy520.DButil.DButil;
import top.hzy520.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class loginDaoImpl implements loginDao {

    @Override
    public User selectUserByUserName(String usernmae) {
        Connection conn=null;
        PreparedStatement stm=null;
        try {
            String sql="SELECT * FROM user WHERE username=?";
            conn=DButil.initConnection();
            stm=conn.prepareStatement(sql);
            stm.setString(1,usernmae);
            ResultSet resultSet=stm.executeQuery();
            User user=new User();
            if (resultSet.next()){
            user.setId(resultSet.getLong("id"));
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            user.setHeadimage(resultSet.getString("headimage"));
            user.setRealname(resultSet.getString("realname"));
            }
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
